import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:flutterdemo/pages/tab1/tab1.dart';
import 'package:flutterdemo/pages/tab2/tab2.dart';
import 'package:flutterdemo/pages/tab3/tab3.dart';
import 'package:flutterdemo/pages/tab4/tab4.dart';
import 'package:flutterdemo/pages/tab5/tab5.dart';

class TabNavigator extends StatefulWidget {
  final entercurrentIndex;
  TabNavigator({Key key, this.entercurrentIndex = 0}) : super(key: key);

  _TabNavigatorState createState() => _TabNavigatorState();
}

class _TabNavigatorState extends State<TabNavigator> {
  int _currentIndex;
  PageController _controller;
  static Future<void> pop() async {
    await SystemChannels.platform.invokeMethod('SystemNavigator.pop');
  }

  @override
  void initState() {
    super.initState();
    _currentIndex = widget.entercurrentIndex;
    print('widget.entercurrentIndex == ${widget.entercurrentIndex}');
    _controller = PageController(
      initialPage: _currentIndex,
      //类似于keepalive
      keepPage: true,
    );
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () {
        _openalertdialog();
        return;
      },
      child: Scaffold(
        // pageView 定义滑动页面
        body: PageView(
          controller: _controller,
          // 控制pageView 水平还是垂直滑动
          // scrollDirection: Axis.vertical,
          // 页面切换时是否有回弹，默认true，
          // pageSnapping: false,

          // pageview 页面改变时触发；
          onPageChanged: (int index) {
            setState(() {
              _currentIndex = index;
            });
          },
          children: <Widget>[
            Tab1(),
            Tab2(),
            Tab3(),
            Tab4(),
            Tab5(),
          ],
        ),
        bottomNavigationBar: BottomNavigationBar(
          currentIndex: _currentIndex,
          onTap: (index) {
            _controller.jumpToPage(index);
            setState(() {
              _currentIndex = index;
            });
          },
          type: BottomNavigationBarType.fixed,
          selectedFontSize: 12.0,
          selectedItemColor: Colors.red,
          unselectedItemColor: Color.fromRGBO(144, 149, 170, 1),
          backgroundColor: Color.fromRGBO(255, 255, 255, 1),
          // elevation: 0.0,
          items: [
            BottomNavigationBarItem(
              icon: Icon(
                Icons.ac_unit,
                size: 24.0,
              ),
              activeIcon: Icon(
                Icons.ac_unit,
                size: 24.0,
                color: Colors.red,
              ),
              label: '布局',
            ),
            BottomNavigationBarItem(
              icon: Icon(
                Icons.access_alarm,
                size: 24.0,
              ),
              activeIcon: Icon(
                Icons.access_alarm,
                size: 24.0,
                color: Colors.red,
              ),
              label: '路由管理',
            ),
            BottomNavigationBarItem(
              icon: Icon(
                Icons.fast_forward,
                size: 24.0,
              ),
              activeIcon: Icon(
                Icons.fast_forward,
                size: 24.0,
                color: Colors.red,
              ),
              label: '状态管理',
            ),
            BottomNavigationBarItem(
              icon: Icon(
                Icons.tag_faces,
                size: 24.0,
              ),
              activeIcon: Icon(
                Icons.tag_faces,
                size: 24.0,
                color: Colors.red,
              ),
              label: 'dart语法',
            ),
            BottomNavigationBarItem(
              icon: Icon(
                Icons.pages,
                size: 24.0,
              ),
              activeIcon: Icon(
                Icons.pages,
                size: 24.0,
                color: Colors.red,
              ),
              label: '混合开发',
            ),
          ],
        ),
      ),
    );
  }

  void _openalertdialog() async {
    final res = await showDialog(
      context: context,
      builder: (context) => AlertDialog(
        title: Text('你确定要退出App吗？'),
        actions: <Widget>[
          RaisedButton(
              child: Text('退出'),
              onPressed: () => Navigator.of(context).pop("yes")),
          RaisedButton(
              child: Text('取消'),
              onPressed: () => Navigator.of(context).pop("no")),
        ],
      ),
    );
    switch (res) {
      case 'no':
        print('no');
        break;
      case 'yes':
        print('yes');
        await pop();
        break;
    }
  }
}
